package com.lenovo.leos.letools.utils;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.util.Log;

import com.lenovo.leos.letools.domain.Folder;
import com.lenovo.leos.letools.domain.LoadFile;

public class JSONParser {
	/**
	 * 解析login操作的返回值
	 * 
	 * @param retValue
	 * @return
	 *{"result":1,"name":"ccyyss","filesize":1024,"dirid":"1301279145610203981","status":"4","issubaccount":0}
	 *{"result":0,"errmsg":"用户不存在"}
	 */
	public static ContentValues parseLogin(String retValue) throws Exception{
		ContentValues cv = new ContentValues();
		try {
			JSONObject obj = new JSONObject(retValue);
			int resultVal = (Integer) obj.get("result");
			if (resultVal == 0) {//返回错误信息
				String errorInf = (String)obj.get("errmsg");
				throw new Exception(URLDecoder.decode(errorInf));//解析url编码
				
			} else {
				cv.put("username",obj.get("name").toString());
				cv.put("rootid",obj.get("dirid").toString());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return cv;
	}
	/**
	 * 解析getspace操作的返回值
	 * 
	 * @param retValue
	 * @return
	 *{"result":1,"totalspace":"10737418240","space":"41232954"}
	 *{"result":0,"errmsg":""}
	 */
	public static ContentValues parseGetspace(String retValue) throws Exception{
		ContentValues cv = new ContentValues();
		try {
			JSONObject obj = new JSONObject(retValue);
			int resultVal = (Integer) obj.get("result");
			if (resultVal == 0) {//返回错误信息
				String errorInf = (String)obj.get("errmsg");
				throw new Exception(URLEncoder.encode(errorInf));
			} else {
				cv.put("totalspace",obj.get("totalspace").toString());
				cv.put("space",obj.get("space").toString());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return cv;
	}
	/**
	 * 解析ListDir操作的返回值
	 * 
	 * @param jsonstr
	 * @return
	 * {"result":1,"list":[{"dirid":"1301637937084203524","dirname":"aaa","privilege":"65535","ownername":"","isshare":"0"}],"files":4}
	 * {"result":1,"list":[],"files":4}
	 * {"result":0,"errmsg":"something to tell user"} 
	 */
	public static List<Folder> parseListdirs(String dirInfObj) throws Exception{
		List<Folder> folders = null;
		try {
			JSONObject obj = new JSONObject(dirInfObj);
			int resultVal = (Integer) obj.get("result");
			if (resultVal == 0) {//返回错误信息
				String errorInf = (String)obj.get("errmsg");
				throw new Exception(URLEncoder.encode(errorInf));
			} else {
				JSONArray dirArray = (JSONArray) obj.get("list");
				int count = dirArray.length();// 得到数组元素个数
				if (count == 0){
					return null;// 指定文件夹下没有子文件夹
				}
				folders = new ArrayList<Folder>();
				for (int i = 0; i <count; i++) {
					JSONObject dirObj = (JSONObject) dirArray.get(i);
					String dirid = (String) dirObj.get("dirid");
					String dirname = (String)dirObj.get("dirname");
					folders.add(new Folder(dirid,dirname));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return folders;
	}
	/**
	 * 解析ListFile操作的返回值
	 *	{"result":1,"list":[
	 *	  {"fileid":"1301637353565203464","name":"DSCN5016.JPG","size":"1654252","time":"2011-04-01 13:55:53","modifytime":"2008-01-01 00:00:00","memo":"","state":"0","sec":"0","verifycode":"","logourl":"/images/file_type/list_pic.gif"},
	 *    {"fileid":"1301561403156209816","name":"hongchen.jpg","size":"1890410","time":"2011-03-31 16:50:03","modifytime":"2008-01-01 00:00:00","memo":"","state":"0","sec":"0","verifycode":"","logourl":"/images/file_type/list_pic.gif"}
	 *                     ]
	 *	}
	 *	{"result":1,"list":[]}
	 * @return
	 */
	public static List<LoadFile> parseListFile(String fileInfObj) throws Exception{
		List<LoadFile> LoadFiles = new ArrayList<LoadFile>();
		try {
			JSONObject obj = new JSONObject(fileInfObj);
			int resultVal = (Integer) obj.get("result");
			if (resultVal == 0) {//返回错误信息
				String errorInf = (String)obj.get("errmsg");
				throw new Exception(URLEncoder.encode(errorInf));
			} else {
				JSONArray dirArray = (JSONArray) obj.get("list");
				int count = dirArray.length();// 得到数组元素个数
				if (count == 0){
					return null;// 指定文件夹下没有子文件
				}
				for (int i = 0; i <count; i++) {
					JSONObject dirObj = (JSONObject) dirArray.get(i);
					String fileid = (String) dirObj.get("fileid");
					String name = (String)dirObj.get("name");
					String fileLen = (String)dirObj.get("size");
					LoadFiles.add(new LoadFile(fileid,name,fileLen));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return LoadFiles;
	}
	/**
	 * 解析createDir操作返回的值
	 * @param dir
	 * @return
	 * {"result":1,"id":"1302080563910209440","rootid":"1302080563910209440","type":"folder","privilege":"1111111111111111"}
	 * @throws Exception 
	 */
	public static Folder parseDir(String dir) throws Exception{
		JSONObject obj = null;
		Folder folder = new Folder();
		try {
			obj = new JSONObject(dir);
			int resultVal = obj.getInt("result");
			if(resultVal == 0 ){
				String errorInf = (String)obj.get("errmsg");
				Log.d("JSONParser","################errorInf is: " + errorInf);
//				throw new Exception(URLEncoder.encode(errorInf));
			}
			String id = obj.getString("id");
			folder.setId(id);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return folder;
	}
}
